<template>
  <div>
    <BasicTable @register="registerTable">
      <template #toolbar>
        <a-button type="primary" @click="handleCreate" v-if="hasPermission('free:column:add')">
          新增字段
        </a-button>
      </template>
      <template #action="{ record }">
        <TableAction
          :actions="[
            {
              auth: 'free:column:edit',
              icon: 'ant-design:read-outlined',
              ifShow: ['radio', 'select'].includes(record.htmlType),
              onClick: handleDictEdit.bind(null, record),
              tooltip: '配置字典值',
            },
            {
              auth: 'free:column:edit',
              icon: 'clarity:note-edit-line',
              onClick: handleEdit.bind(null, record),
            },
            {
              auth: 'free:column:remove',
              icon: 'ant-design:delete-outlined',
              color: 'error',
              popConfirm: {
                title: '是否确认删除',
                confirm: handleDelete.bind(null, record),
              },
            },
          ]"
        />
      </template>
    </BasicTable>
    <ColumnDrawer @register="registerDrawer" @success="handleSuccess" />
    <DictModal @register="registerModal" />
  </div>
</template>
<script lang="ts">
  import { defineComponent } from 'vue';

  import { BasicTable, useTable, TableAction } from '/@/components/Table';
  import { getColumnListByPage, removeColumn } from '/@/api/free/column';

  import { useDrawer } from '/@/components/Drawer';
  import ColumnDrawer from './ColumnDrawer.vue';

  import { useModal } from '/@/components/Modal';
  import DictModal from './DictModal.vue';

  import { columns, searchFormSchema } from './column.data';
  import { usePermission } from '/@/hooks/web/usePermission';
  import { useMessage } from '/@/hooks/web/useMessage';
  import { useRoute } from 'vue-router';

  export default defineComponent({
    name: 'ColumnManagement',
    components: { BasicTable, ColumnDrawer, DictModal, TableAction },
    setup() {
      const { createErrorModal } = useMessage();
      const { tableName } = useRoute().query;
      if (!tableName) {
        createErrorModal({ title: '当前链接不正确', content: '缺少tableName参数，请检查' });
      }
      searchFormSchema[0].defaultValue = tableName;
      const { hasPermission } = usePermission();
      const [registerDrawer, { openDrawer }] = useDrawer();
      const [registerModal, { openModal }] = useModal();
      const [registerTable, { reload }] = useTable({
        title: '字段列表',
        api: getColumnListByPage,
        immediate: true, // true 立即请求接口， false 需调用reload加载
        columns,
        formConfig: {
          labelWidth: 120,
          schemas: searchFormSchema,
        },
        useSearchForm: true,
        showTableSetting: true,
        bordered: true,
        showIndexColumn: false,
        actionColumn: {
          width: 120,
          title: '操作',
          dataIndex: 'action',
          slots: { customRender: 'action' },
          fixed: undefined,
        },
      });

      function handleCreate() {
        openDrawer(true, {
          isUpdate: false,
          tableName,
        });
      }

      function handleEdit(record: Recordable) {
        openDrawer(true, {
          record,
          isUpdate: true,
        });
      }

      function handleDictEdit(record: Recordable) {
        openModal(true, {
          columnId: record.id,
        });
      }

      function handleDelete(record: Recordable) {
        removeColumn(record.id).then(() => {
          reload();
        });
      }

      function handleSuccess() {
        reload();
      }

      return {
        hasPermission,
        registerTable,
        registerDrawer,
        registerModal,
        handleCreate,
        handleDictEdit,
        handleEdit,
        handleDelete,
        handleSuccess,
      };
    },
  });
</script>
